<?php
namespace app\manage\model;
use think\Db;
use think\Model;

class EstateBill extends Model {
    protected $table = 'estate_bill';
    public function __construct($data = [])
    {
//        parent::__construct($data);
    }

    /**
     * @param $data
     * @return string
     */
    public function m_insert($data) {
        $data = Db::name($this->table)
            ->insert($data);
        return Db::name($this->table)->getLastInsID();
    }

    /**
     * 编辑
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_update($id, $data) {
        $data = Db::name($this->table)
            ->where(['id'=>$id])
            ->update($data);
        return $data;
    }

    /**
     * 唯一性
     * @param $tel
     * @return int|string
     * @throws \think\Exception
     */
    public function uniqueness($number) {
        $where['number'] = $number;
        $data = Db::name($this->table)
            ->where($where)
            ->count();
        return $data;
    }

    /**
     * 获取列表
     * @param $admin_id
     * @param $status
     * @param $tung
     * @param $unit
     * @param $room_number
     * @param $page
     * @param $time
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($admin_id, $status, $tung, $unit, $room_number, $page, $time) {
        $where = [];
        if (isset($admin_id) && $admin_id !== "") {
            $where['admin_id'] = $admin_id;
        }
        if (isset($status) && $status !== "") {
            $where['status'] = $status;
        }
        if (isset($tung) && $tung !== "") {
            $where['tung_num'] = $tung;
        }
        if (isset($unit) && $unit !== "") {
            $where['unit_num'] = $unit;
        }
        if (isset($room_number) && $room_number !== "") {
            $where['room_number'] = $room_number;
        }
        if (isset($time) && $time !== "") {
            $op = 'between';
            switch ($time) {
                case 1:
                    $range = ['first Day of this month 00:00:00', 'first Day of next month 00:00:00'];
                    break;
                case 2:
                    $range = ['first Day of last month 00:00:00', 'first Day of this month 00:00:00'];
                    break;
                case 3:
                    $op = '>';
                    $range = '-3 months';
                    break;
                default:
            }
        }

        $data['data'] = Db::name($this->table)
            ->where($where)
            ->whereTime('create_time', $op, $range)
            ->page($page, get_list_rows())
            ->order('create_time desc')
            ->select();
        $data['total'] = Db::name($this->table)
            ->where($where)
            ->whereTime('create_time', $op, $range)
            ->count();

        return $data;
    }

    /**
     * 获得已缴未缴户数
     * @return mixed
     * @throws \think\Exception
     */
    public function toDo($admin_id) {
        $data['paid'] = Db::name($this->table)
            ->where('status', '<>', '1')
            ->where('admin_id', $admin_id)
            ->whereTime('create_time', 'month')
            ->count();
        $data['unpaid'] = Db::name($this->table)
            ->where('status', '=', '1')
            ->whereTime('create_time', 'month')
            ->where('admin_id', $admin_id)
            ->count();
        return $data;
    }

    /**
     * 得到某房源所有未缴账单
     * @param $house_id
     * @param $type
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUnpay($house_id, $type) {
        $where = ['house_id' => $house_id, 'type' => $type];
        $data = Db::name($this->table)
            ->where('status', '1')
            ->where($where)
            ->select();
        if (!empty($data)) {
            $total = 0.00;
            $a = [];
            foreach ($data as $value) {
                $a['id'] = $value['id'];
                $a['type'] = 5;
                $a['tung_num'] = $value['tung_num'];
                $a['unit_num'] = $value['unit_num'];
                $a['room_number'] = $value['room_number'];
                $a['tenant_name'] = $value['tenant_name'];
                $total = $total + $value['cost'];
            }
            $a['cost'] = $total;
            return $a;
        } else {
            return $data;
        }
    }

    /**
     * 根据out_trade_no找到对应记录
     * @param $out_trade_no
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index_out_trade_no($out_trade_no) {
        $where['out_trade_no'] = $out_trade_no;
        $data = Db::name($this->table)
            ->where($where)
            ->select();
        return $data;
    }

    /**
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($id) {
        $where['id'] = $id;
        $data = Db::name($this->table)
            ->where($where)
            ->find();
        return $data;
    }

    /**
     * @param $tenant_id
     * @param string $status
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index_tenant_id($tenant_id, $page='', $status='') {
        if (!empty($status)) {
            if ($status == 1) {
                $where['status'] = 1;
            } else {
                $where['status'] = ['<>', 1];
            }
        }
        $where['tenant_id'] = $tenant_id;
        $data['data'] = Db::name($this->table)
            ->where($where)
            ->page($page, get_list_rows())
            ->order('time desc create_time desc')
            ->select();
        $data['total'] = Db::name($this->table)
            ->where($where)
            ->count();
        return $data;
    }

    /**
     * @param $tung
     * @param $unit
     * @param $room_number
     * @param $time
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function indexTime($tung, $unit, $room_number, $time, $id) {
        $where = [];
        if (isset($tung) && $tung !== "") {
            $where['tung_num'] = $tung;
        }
        if (isset($unit) && $unit !== "") {
            $where['unit_num'] = $unit;
        }
        if (isset($room_number) && $room_number !== "") {
            $where['room_number'] = $room_number;
        }
        if (isset($time) && $time !== "") {
            $y = explode('-', $time)[0];
            $m = explode('-', $time)[1];
            $start = mFristAndLast($y, $m)['firstday'];
            $end = mFristAndLast($y, $m)['lastday'];
            $where['create_time'] = ['between', [$start, $end]];
        }
        if (isset($id) && $id !== '') {
            $where['tenant_id'] = $id;
        }

//        if (isset($time) && $time !== "") {
//            $op = 'between';
//            switch ($time) {
//                case 1:
//                    $range = ['first Day of this month 00:00:00', 'first Day of next month 00:00:00'];
//                    break;
//                case 2:
//                    $range = ['first Day of last month 00:00:00', 'first Day of this month 00:00:00'];
//                    break;
//                case 3:
//                    $op = '>';
//                    $range = '-3 months';
//                    break;
//                default:
//            }
//        }

        $data = Db::name($this->table)
            ->where($where)
//            ->whereTime('create_time', $op, $range)
            ->order('create_time desc')
            ->select();

        foreach ($data as &$a) {
            $a['bill_type'] = 5;
        }
        return $data;
    }
}